package com.springboot.cloud.auth.authentication.events;

import com.springboot.cloud.auth.authentication.provider.UserProvider;
import com.springboot.cloud.auth.authentication.service.IResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
@RabbitListener(queues = "organization.role")
public class RoleReceiver {

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private IResourceService resourceService;
    @RabbitHandler
    public void process(String roleId){
        log.debug("mq 接收到的消息{}",roleId);
        //远程到organization微服中获取用户名  利用不利用缓存手段？
        List<String> usernames = userProvider.getUserNamesByRoleId(roleId).getData();
        log.debug("{}",usernames);
        usernames.forEach(username ->{
            log.debug("清理{}",username);
            resourceService.cleanResource4User(username);
        });

    }
}
